package com.hutou.chuangke.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hutou.chuangke.Bean.AlbnmlistBean;
import com.hutou.chuangke.Bean.MvlistBean;
import com.hutou.chuangke.Bean.SoundBean;
import com.hutou.chuangke.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by o on 2017/4/12.
 */
public class IndexSoundAdapter extends BaseAdapter{
    Context mContext;
    private boolean isLimit = false;//限制最多只有四个
    List<?> list = new ArrayList<>();
    public IndexSoundAdapter(Context context,List<?> list,boolean isLimit){
        this.mContext = context;
        this.list = list;
        this.isLimit = isLimit;
    }

    public boolean isLimit() {
        return isLimit;
    }

    public void setLimit(boolean limit) {
        isLimit = limit;
    }

    @Override
    public int getCount() {
        if(isLimit){
            if(list.size()>6){
                return 6;
            }else {
                return list.size();
            }
        }else {
            return list.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if(isLimit){
            if(position>3){
                return list.get(position);
            }else {
                return list.get(position);
            }
        }else{
            return list.get(position);
        }

    }

    @Override
    public long getItemId(int position) {
        if(position>3){
            return position;
        }else {
            return position;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            convertView = View.inflate(mContext,
                    R.layout.index_gridview_item,null);
            holder = new ViewHolder();
            convertView.setTag(holder);
        } else{
            holder = (ViewHolder) convertView.getTag();
        }
        if(isLimit){
            if(position>5){
                return convertView;
            }
        }
        holder.grid_title = (TextView) convertView.findViewById(R.id.grid_title);
        holder.grid_image = (ImageView) convertView.findViewById(R.id.grid_image);
        holder.grid_subtitle = (TextView) convertView.findViewById(R.id.grid_subtitle);
        if(list.get(position) instanceof MvlistBean){
            MvlistBean mvbean = (MvlistBean) list.get(position);
            holder.grid_title.setText(mvbean.getSoundname());
            holder.grid_subtitle.setText(mvbean.getSinger());
            ImageLoader.getInstance().displayImage(mvbean.getSoundurlimg(), holder.grid_image);
        }else if(list.get(position) instanceof SoundBean){
            SoundBean mvbean = (SoundBean) list.get(position);
            holder.grid_title.setText(mvbean.getSoundname());
            holder.grid_subtitle.setText(mvbean.getSinger());
            ImageLoader.getInstance().displayImage(mvbean.getSoundurlimg(), holder.grid_image);
        }else if(list.get(position) instanceof AlbnmlistBean){
            AlbnmlistBean albnmlistBean = (AlbnmlistBean) list.get(position);
            holder.grid_title.setText(albnmlistBean.getAtitle());
            holder.grid_subtitle.setText(albnmlistBean.getSinger());
            ImageLoader.getInstance().displayImage(albnmlistBean.getAimg(), holder.grid_image);
        }else if(list.get(position) instanceof String){
            ImageLoader.getInstance().displayImage((String) list.get(position), holder.grid_image);
        }
        return convertView;
    }

    private class ViewHolder{
        ImageView grid_image;
        TextView grid_title;
        TextView grid_subtitle;
    }
}
